package cn.edu.zsc.rbac.view;

import cn.edu.zsc.rbac.domain.vo.BaseVo;

import cn.edu.zsc.rbac.domain.vo.Result;
import cn.edu.zsc.rbac.service.IBaseService;


/**
 * 视图层-基类
 *
 * @author 郑植航
 * @since 2025/6/8 14:25
 */
public abstract class BaseView <Vo extends BaseVo,IService extends IBaseService>{

    IService service;
    Vo[] cache;

    void printError(String message) {
        System.out.println("\033[31m" + message + "\033[0m");
    }


    Vo getCacheById() {
        System.out.print("请输入ID，ID必须是大于0的整数：");

        if (!MainView.input.hasNextInt()) {
            MainView.input.nextLine();
            printError("ID必须为整数，请输入整数值");
            return null;
        }

        int id = MainView.input.nextInt();
        MainView.input.nextLine();

        if (id < 1) {
            printError("ID必须大于0，请重新输入");
            return null;
        }

        if (cache == null) {
            Result result = service.list();
            cache = (Vo[]) result.data;
        }

        Vo dept = null;
        for (Vo item : cache) {
            if (item.id == id) {
                System.out.println("该ID对应的数据是：");
                System.out.println(item);
                dept = item;
                break;
            }
        }
        if (dept == null) {
            System.out.println("没有该ID对应的数据");
            return null;
        }
        return dept;
    }
}

